/*
--------------------------------
Ajax Contact Form
--------------------------------
+ https://github.com/pinceladasdaweb/Ajax-Contact-Form
+ A Simple Ajax Contact Form developed in PHP with HTML5 Form validation.
+ Has a fallback in jQuery for browsers that do not support HTML5 form validation.
+ version 1.0.1
+ Copyright 2014 Pedro Rogerio
+ Licensed under the MIT license
+ https://github.com/pinceladasdaweb/Ajax-Contact-Form
*/

(function ($, window, document, undefined) {
    'use strict';

    var $form = $('#apply-form');

    $form.submit(function (e) {
        // remove the error class
        $('.form-group').removeClass('has-error');
        $('.help-block').remove();

        // get the form data
        var formData = {
            'address' : $('input[name="form-address"]').val(),
            'postal' : $('input[name="form-postal"]').val(),
            'invoice' : $('input[name="form-invoice"]').val(),
            'batch' : $('input[name="form-batch"]').val(),
            'email' : $('input[name="form-email"]').val(),
            'phone' : $('input[name="form-phone"]').val(),
            'openid' : $('input[id="openid"]').val(),
            'recordid' : $('input[id="recordid"]').val()
        };

        // process the form
        $.ajax({
            type : 'GET',
            url  : '/personal/buy/apply/submit',
            data : formData,
            contentType:'application/json',
            encode : true
        }).done(function (data) {
            window.location.href = "/personal/buy/list?openid="+$('input[id="openid"]').val();
            //$form.html('<div class="alert alert-success">' + data.msg + '</div>');
        }).fail(function (data) {
            // for debug
            console.log(data)
        });

        e.preventDefault();
    });
}(jQuery, window, document));
